package com.ijuit.web;

import com.ijuit.business.interfaces.user.UserService;
import com.ijuit.model.user.User;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;
/**
 * 首页
 * @author 罗龙
 *
 */
@Before(IocInterceptor.class)  
public class IndexAction extends Controller {
	
	@Inject.BY_NAME
    private UserService userService; 
	
	/**
	 * 进入首页
	 */
	public void index(){
		render("index.html");
	}
	
	/**
	 * 登陆
	 * @throws Exception 
	 */
	public void login() throws Exception{
		String loginId=getPara("loginId");  
        String password=getPara("password");
        try {
        	 int code = userService.login(loginId, password);
             if(code==1){  
                 renderText("登录成功！");  
             }else{  
                 renderText("登录失败！");  
             }  
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 注册
	 */
	public void register(){
		boolean flag = false;
		try {
			User user =	getModel(User.class);
			if(User.me.exist(user.getStr("loginId"))){
				renderText("账号已被注册！");
				return;
			}
			if(User.me.exist(user.getStr("mobileTelephone"))){
				renderText("手机号已被注册！");
				return;
			}
			if(User.me.exist(user.getStr("email"))){
				renderText("邮箱已被注册！");
				return;
			}
			flag = userService.register(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(flag){
			  renderText("注册成功"); 
		}else{
			renderText("注册失败"); 
		}
	}
}
